import 'common/util/math.js';
import { Message } from 'element-ui';
import ProjectBudgetContent from './BudgetOrderList/BudgetOrderList.vue';
import AddProjectBudget from './AddProjectBudgetDialog/AddProjectBudgetDialog.vue';
import selectProjectBudget from './SelectProjectBudgetDialog/SelectProjectBudgetDialog.vue';
import CommonPaging from 'components/common/paging/index.vue';

export default {
    name: 'projectBudgetDetail',
    components: {
        ProjectBudgetContent,
        AddProjectBudget,
        selectProjectBudget,
        CommonPaging
    },
    data() {
        return {
            projectBudgetList: [],
            searchData: {
                pageNum: 1,
                pageSize: 20,
                workOrderNumber: '',
                constructionProjectName: ''
            },
            isAdd: false,
            dataTotal: 0,
            currentRow: null,
            currentOrderRow: null,
            currentOrderInd: -1,
            budgetDialogData: {
                constructionProjectName: '',
                constructionProjectArea: '',
                constructionProjectAddress: '',
                constructionType: '',
                designFiles: '',
                id: '',
                invoiceTaxRate: '',
                workOrderNumber: '',
                budgetType: '',
                projectBudget: '',
                budgetDetailTemplate: [],
                budgetDetailCustom: [],
                amountExcludingTax: '',
                yearBudgetNumber: ''
            },
            loading: false,
            showProjectBudget: true,
            showBudgetDialog: false,
            btns: [
                { label: '新增', click: this.add },
                { label: '修改', click: this.modify },
                { label: '下载预算清单', click: this.down }
            ]
        };
    },
    mounted() {
        this.getProjectBudgetList();
    },
    methods: {
        // 获取工程预算清单列表数据
        async getProjectBudgetList() {
            this.loading = true;
            const res = await this.$httpExt().post('/api/budget/queryList', this.searchData);
            this.loading = false;
            this.dataTotal = 1;
            if (res.data) {
                this.projectBudgetList = res.data.list;
            }
        },
        // 新增
        async add() {
            const addProjectBudgetDialog = this.$refs.addProjectBudgetDialog;
            addProjectBudgetDialog.showDialog();
        },
        rowSelected() {
            if (!this.currentRow) {
                Message({
                    type: 'info',
                    message: '请选择一条数据'
                });
                return false;
            }
            return true;
        },
        async modify() {
            if (!this.rowSelected()) return;
            const { selectProjectBudgetDialog } = this.$refs;
            selectProjectBudgetDialog.showDialog();
            const res = await this.$httpExt().get(`/api/budget/queryOne/${this.currentRow.id}`);
            const data = res.data || {};
            const baseInfoFields = [
                'constructionProjectName',
                'constructionProjectAddress',
                'constructionProjectArea',
                'constructionType',
                'supplierType',
                'budgetType',
                'invoiceTaxRate',
                'id',
                'workOrderNumber',
                'designFiles',
                'supplierName',
                'supplierCode'
            ];
            baseInfoFields.forEach(field => (this.budgetDialogData[field] = data[field]));
            data.cbdList.forEach(row => {
                row.init = true; // 为初始化数据添加标识，
                if (row.detailType === '1') {
                    this.budgetDialogData.budgetDetailTemplate.push(row);
                } else if (row.detailType === '2') {
                    this.budgetDialogData.budgetDetailCustom.push(row);
                }
            });
            const { budgetDetailTemplate, budgetDetailCustom } = this.budgetDialogData;
            const { selectProjectBudgetDialog: dialog } = this.$refs;
            // 表格有数据，则不能进行
            dialog.uploadBtnDisabled.budgetDetailTemplate = !!budgetDetailTemplate.length;
            dialog.uploadBtnDisabled.budgetDetailCustom = !!budgetDetailCustom.length;

            const suppleFormFields = ['projectBudget', 'amountExcludingTax'];
            suppleFormFields.forEach(field => (this.budgetDialogData[field] = data[field]));

            this.budgetDialogData.invoiceTaxRate = data.invoiceTaxRate || 0;
        },
        down() {},

        handleChangePage(data) {
            this.searchData.pageNum = data.pageNum;
            this.searchData.pageSize = data.pageSize;
            this.getProjectBudgetList();
        },

        handleChangeOrderState() {},

        async handleSearchLogDetail() {
            let res = {};
            const logDetail = this.$refs.logDetail;
            logDetail.showDialog(res);
        },
        // 选择一条工单
        choose(row) {
            if (!row) {
                Message({
                    type: 'info',
                    message: '请选择其中一项'
                });
                return;
            }
            this.currentOrderRow = row;

            this.budgetDialogData = {
                ...this.budgetDialogData,
                ...row
            };
            const { selectProjectBudgetDialog } = this.$refs;
            selectProjectBudgetDialog.isAdd = true;
            selectProjectBudgetDialog.showDialog();
        }
    }
};
